package com.calf.module.common.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;
import com.calf.cn.utils.OSSUtil;
import com.calf.cn.utils.SFtpUtil;
import com.calf.cn.utils.UUIDUtils;
import com.calf.module.common.entity.FileUpload;

/**
 * 图片上传控制类
 * 
 * @author guixin
 *
 */
@Controller
@RequestMapping(value = "/upload")
public class ImgUploadController {

	private static String webPath = "";

	@ResponseBody
	@RequestMapping("/appversion.htm")
	public FileUpload appversionkUpload(String id,
			@RequestParam("appversionFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个文件上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			String savepath = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				savepath = webPath + "file" + File.separator
						+ uuid.substring(0, 3) + File.separator + file.getOriginalFilename();
				File dir = new File(savepath);

				if (!dir.isDirectory()) {
					dir.mkdirs();
				}
				try {
					file.transferTo(dir);

				} catch (Exception e) {
					e.printStackTrace();
				}

				String dirs = uuid.substring(0, 3);

				String path = "";
				path = "/images/calfloan/strategy/";
				SFtpUtil sftp = SFtpUtil.getSFtpUtil();
				sftp.createDir(path, dirs);

				imgurl = "http://strategy.xnsudai.com/" + dirs + "/" + file.getOriginalFilename();
				sftp.upload(path + dirs + "/", savepath, file.getOriginalFilename());
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl(imgurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}
	
	/**
	 * icon上传
	 * 
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("/banner.htm")
	public FileUpload bannerUpload(String id,
			@RequestParam("bannerFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");
		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			String ip = getRemoteHost(request);
			String ipAddress = ip == null ? "" : ip.replace(".", "");
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String uuid = UUIDUtils.getUUID().toLowerCase();
			String fileName= file.getOriginalFilename();
			String suffix = fileName.substring(fileName.lastIndexOf(".")); 
			String imageName = ipAddress+uuid+suffix;
			OSSClient ossClient = OSSUtil.getOSSClient();
			boolean flag = OSSUtil.uploadInputStreamObject2OSS(ossClient, file.getInputStream(),imageName, "test-sudai", "admin");
			if(flag){
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl("/"+"admin"+"/"+imageName);
				upload.setImgName(imageName);
			}
		}
		return upload;
	}

	@ResponseBody
	@RequestMapping("/loancate.htm")
	public FileUpload loancateUpload(String id,
			@RequestParam("loancateFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			String savepath = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				savepath = webPath + "image_space" + File.separator
						+ uuid.substring(0, 3) + File.separator + uuid + ".jpg";
				File dir = new File(savepath);

				if (!dir.isDirectory()) {
					dir.mkdirs();
				}
				try {
					file.transferTo(dir);

				} catch (Exception e) {
					e.printStackTrace();
				}

				String dirs = uuid.substring(0, 3);

				String path = "";
				path = "/images/calfloan/loan/";
				SFtpUtil sftp = SFtpUtil.getSFtpUtil();
				sftp.createDir(path, dirs);

				imgurl = "http://loan.xnsudai.com/" + dirs + "/" + uuid
						+ ".jpg";
				sftp.upload(path + dirs + "/", savepath, uuid + ".jpg");
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl(imgurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}

	@ResponseBody
	@RequestMapping("/loaninfo.htm")
	public FileUpload loaninfoUpload(String id,
			@RequestParam("loaninfoFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				String dirs = uuid.substring(0, 3);
				String fileName= file.getOriginalFilename();
				String suffix = fileName.substring(fileName.lastIndexOf(".")); 
				OSSClient ossClient = OSSUtil.getOSSClient();
				boolean flag = OSSUtil.uploadInputStreamObject2OSS(ossClient, file.getInputStream(), dirs+suffix, "test-sudai", "admin");
				if(flag){
					upload.setResult("success");
					upload.setMsg("上传图片成功！");
					upload.setImgUrl(imgurl);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}

	@ResponseBody
	@RequestMapping("/applyprocess.htm")
	public FileUpload applyprocessUpload(String id,
			@RequestParam("applyprocessFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			String savepath = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				savepath = webPath + "image_space" + File.separator
						+ uuid.substring(0, 3) + File.separator + uuid + ".jpg";
				File dir = new File(savepath);

				if (!dir.isDirectory()) {
					dir.mkdirs();
				}
				try {
					file.transferTo(dir);

				} catch (Exception e) {
					e.printStackTrace();
				}

				String dirs = uuid.substring(0, 3);

				String path = "";
				path = "/images/calfloan/loan/";
				SFtpUtil sftp = SFtpUtil.getSFtpUtil();
				sftp.createDir(path, dirs);

				imgurl = "http://loan.xnsudai.com/" + dirs + "/" + uuid
						+ ".jpg";
				sftp.upload(path + dirs + "/", savepath, uuid + ".jpg");
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl(imgurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}

	//银行上传
	@ResponseBody
	@RequestMapping("/bank.htm")
	public FileUpload bankUpload(String id,
			@RequestParam("bankFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			String savepath = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				savepath = webPath + "image_space" + File.separator
						+ uuid.substring(0, 3) + File.separator + uuid + ".jpg";
				File dir = new File(savepath);

				if (!dir.isDirectory()) {
					dir.mkdirs();
				}
				try {
					file.transferTo(dir);

				} catch (Exception e) {
					e.printStackTrace();
				}

				String dirs = uuid.substring(0, 3);

				String path = "";
				path = "/images/calfloan/credit/";
				SFtpUtil sftp = SFtpUtil.getSFtpUtil();
				sftp.createDir(path, dirs);

				imgurl = "http://credit.xnsudai.com/" + dirs + "/" + uuid
						+ ".jpg";
				sftp.upload(path + dirs + "/", savepath, uuid + ".jpg");
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl(imgurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}
	//信用卡上传
	@ResponseBody
	@RequestMapping("/creditCard.htm")
	public FileUpload creditCardUpload(String id,
			@RequestParam("creditCardFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			String savepath = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				savepath = webPath + "image_space" + File.separator
						+ uuid.substring(0, 3) + File.separator + uuid + ".jpg";
				File dir = new File(savepath);

				if (!dir.isDirectory()) {
					dir.mkdirs();
				}
				try {
					file.transferTo(dir);

				} catch (Exception e) {
					e.printStackTrace();
				}

				String dirs = uuid.substring(0, 3);

				String path = "";
				path = "/images/calfloan/credit/";
				SFtpUtil sftp = SFtpUtil.getSFtpUtil();
				sftp.createDir(path, dirs);

				imgurl = "http://credit.xnsudai.com/" + dirs + "/" + uuid
						+ ".jpg";
				sftp.upload(path + dirs + "/", savepath, uuid + ".jpg");
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl(imgurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}
	
	/*********************************/
	//信用卡上传背景图
	@ResponseBody
	@RequestMapping("/creditBackCard.htm")
	public FileUpload creditCardUploadBack(String id,
			@RequestParam("creditCardFileBack") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			String savepath = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				savepath = webPath + "image_space" + File.separator
						+ uuid.substring(0, 3) + File.separator + uuid + ".jpg";
				File dir = new File(savepath);

				if (!dir.isDirectory()) {
					dir.mkdirs();
				}
				try {
					file.transferTo(dir);

				} catch (Exception e) {
					e.printStackTrace();
				}

				String dirs = uuid.substring(0, 3);

				String path = "";
				path = "/images/calfloan/credit/";
				SFtpUtil sftp = SFtpUtil.getSFtpUtil();
				sftp.createDir(path, dirs);

				imgurl = "http://credit.xnsudai.com/" + dirs + "/" + uuid
						+ ".jpg";
				sftp.upload(path + dirs + "/", savepath, uuid + ".jpg");
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl(imgurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}
	
	//主题卡
	@ResponseBody
	@RequestMapping("/themeType.htm")
	public FileUpload themeTypeUpload(String id,
			@RequestParam("themeTypeFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			String savepath = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				savepath = webPath + "image_space" + File.separator
						+ uuid.substring(0, 3) + File.separator + uuid + ".jpg";
				File dir = new File(savepath);

				if (!dir.isDirectory()) {
					dir.mkdirs();
				}
				try {
					file.transferTo(dir);

				} catch (Exception e) {
					e.printStackTrace();
				}

				String dirs = uuid.substring(0, 3);

				String path = "";
				path = "/images/calfloan/credit/";
				SFtpUtil sftp = SFtpUtil.getSFtpUtil();
				sftp.createDir(path, dirs);

				imgurl = "http://credit.xnsudai.com/" + dirs + "/" + uuid
						+ ".jpg";
				sftp.upload(path + dirs + "/", savepath, uuid + ".jpg");
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl(imgurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}
	
	
	@ResponseBody
	@RequestMapping("/appinfo.htm")
	public FileUpload appinfoUpload(String id,
			@RequestParam("appinfoFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			String savepath = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				savepath = webPath + "image_space" + File.separator
						+ uuid.substring(0, 3) + File.separator + uuid + ".jpg";
				File dir = new File(savepath);

				if (!dir.isDirectory()) {
					dir.mkdirs();
				}
				try {
					file.transferTo(dir);

				} catch (Exception e) {
					e.printStackTrace();
				}

				String dirs = uuid.substring(0, 3);

				String path = "";
				path = "/images/calfloan/loan/";
				SFtpUtil sftp = SFtpUtil.getSFtpUtil();
				sftp.createDir(path, dirs);

				imgurl = "http://loan.xnsudai.com/" + dirs + "/" + uuid
						+ ".jpg";
				sftp.upload(path + dirs + "/", savepath, uuid + ".jpg");
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl(imgurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}
	
	@ResponseBody
	@RequestMapping("/raiderscate.htm")
	public FileUpload raiderscateUpload(String id,
			@RequestParam("raiderscateFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			String savepath = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				savepath = webPath + "image_space" + File.separator
						+ uuid.substring(0, 3) + File.separator + uuid + ".jpg";
				File dir = new File(savepath);

				if (!dir.isDirectory()) {
					dir.mkdirs();
				}
				try {
					file.transferTo(dir);

				} catch (Exception e) {
					e.printStackTrace();
				}

				String dirs = uuid.substring(0, 3);

				String path = "";
				path = "/images/calfloan/strategy/";
				SFtpUtil sftp = SFtpUtil.getSFtpUtil();
				sftp.createDir(path, dirs);

				imgurl = "http://strategy.xnsudai.com/" + dirs + "/" + uuid
						+ ".jpg";
				sftp.upload(path + dirs + "/", savepath, uuid + ".jpg");
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl(imgurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}
	
	@ResponseBody
	@RequestMapping("/raidersdetail.htm")
	public FileUpload raidersdetailUpload(String id,
			@RequestParam("raidersdetailFile") MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");

		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			String savepath = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				savepath = webPath + "image_space" + File.separator
						+ uuid.substring(0, 3) + File.separator + uuid + ".jpg";
				File dir = new File(savepath);

				if (!dir.isDirectory()) {
					dir.mkdirs();
				}
				try {
					file.transferTo(dir);

				} catch (Exception e) {
					e.printStackTrace();
				}

				String dirs = uuid.substring(0, 3);

				String path = "";
				path = "/images/calfloan/strategy/";
				SFtpUtil sftp = SFtpUtil.getSFtpUtil();
				sftp.createDir(path, dirs);

				imgurl = "http://strategy.xnsudai.com/" + dirs + "/" + uuid
						+ ".jpg";
				sftp.upload(path + dirs + "/", savepath, uuid + ".jpg");
				upload.setResult("success");
				upload.setMsg("上传图片成功！");
				upload.setImgUrl(imgurl);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}

	
	@ResponseBody
	@RequestMapping("/upload.htm")
	public FileUpload upload(String id, MultipartFile file,
			HttpServletRequest request) throws IOException {
		FileUpload upload = new FileUpload("error", "上传图片失败", null,"");
		if (file == null) {
			upload.setMsg("请选择一个图片上传！");
		} else {
			ServletContext context = request.getSession().getServletContext();
			webPath = context.getRealPath("/");
			String imgurl = "";
			try {
				String uuid = UUIDUtils.getUUID().toLowerCase();
				String dirs = uuid.substring(0, 3);
				String fileName= file.getOriginalFilename();
				String suffix = fileName.substring(fileName.lastIndexOf(".")); 
				OSSClient ossClient = OSSUtil.getOSSClient();
				boolean flag = OSSUtil.uploadInputStreamObject2OSS(ossClient, file.getInputStream(), dirs+suffix, "test-sudai", "admin");
				if(flag){
					upload.setResult("success");
					upload.setMsg("上传图片成功！");
					upload.setImgUrl(imgurl);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return upload;
	}
	
	
	@ResponseBody
	@RequestMapping("/type.htm")
	public void type(){
		List<String> list = new ArrayList<String>();
		list.add("image1");
		list.add("image2");
		list.add("image3");
	}
	/**
	 * 获取客户的真实IP地址
	 * 
	 * @param request
	 * @return
	 */
	public String getRemoteHost(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip.equals("0:0:0:0:0:0:0:1") ? null : ip;
	}
}
